package com.ph.irp.base.stockprice.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ph.irp.base.stockprice.dao.StockPriceDAO;
import com.ph.irp.base.stockprice.service.StockPriceService;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprogectSupplierDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectMaterialDao;
import com.ph.irp.dto.StockPriceDTO;
import com.ph.irp.model.PBaseStockprice;
import com.ph.irp.model.PBaseVerifyprojectMaterial;
import com.ph.irp.model.PBaseVerifyprojectSupplier;

public class StockPriceServiceImpl implements StockPriceService {
	
	//采购价格资料dao
	private StockPriceDAO stockPriceDAOProxy = null ;
	//核算项目供应商dao
	private PBaseVerifyprogectSupplierDao supplierDaoProxy = null ;
	//核算项目物料dao
	private PBaseVerifyprojectMaterialDao materialDaoProxy = null ;
	/**
	 * 新增采购价格资料
	 */	
	@Override
	public void addStockPrice(PBaseStockprice pbaseStockPriceModel) throws Throwable{
		// TODO Auto-generated method stub
		
		this.stockPriceDAOProxy.addStockPrice(pbaseStockPriceModel) ;
		
	}
	/**
	 * 批量删除采购价格资料
	 */
	@Override
	public void deleteStockPriceByIds(Object[] stockPriceIds) throws Throwable{
		// TODO Auto-generated method stub
		this.stockPriceDAOProxy.deleteStockPriceByIds(stockPriceIds);

	}
	/**
	 * 根据id值查询采购价格资料
	 */
	@Override
	public PBaseStockprice getStockPriceById(Integer stockPriceId) throws Throwable{
		// TODO Auto-generated method stub
			PBaseStockprice pbaseStockPrceModel = null ;
			pbaseStockPrceModel = this.stockPriceDAOProxy.getStockPriceById(stockPriceId) ;
		return pbaseStockPrceModel;
	}
	/**
	 * 根据采购价格资料id查询采购价格资料dto
	 */
	@Override
	public StockPriceDTO getStockPriceDTOById(int stockpriceId) throws Throwable{
		// TODO Auto-generated method stub
		StockPriceDTO stockPriceDTO = new StockPriceDTO();
		//从数据库中查询得到销售价格资料model
		PBaseStockprice pbaseStockPrceModel = null ;
		pbaseStockPrceModel = this.stockPriceDAOProxy.getStockPriceById(stockpriceId) ;
		//从数据库中查询得到物料model
		PBaseVerifyprojectMaterial materialModel = null ;
		int materialId = pbaseStockPrceModel.getPBaseVerifyprojectMaterialId() ;
		try {
			materialModel = materialDaoProxy.getMaterialById(materialId) ;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//从数据库中查询得到供应商model
		
		PBaseVerifyprojectSupplier supplierModel = supplierDaoProxy.getSupplierById(pbaseStockPrceModel.getPBaseVerifyprojectSupplierId()) ;
		//封装dto
		stockPriceDTO.setStockpriceModel(pbaseStockPrceModel) ;
		stockPriceDTO.setMaterialModel(materialModel) ;
		stockPriceDTO.setSupplierModel(supplierModel) ;
		return stockPriceDTO;
	}	
	
	/**
	 * 动态查询采购价格资料
	 */
	@Override
	public List<StockPriceDTO> getStockPrices(PBaseStockprice pbaseStockPriceModel) throws Throwable{
		// TODO Auto-generated method stub
		//采购价格DTOLIST
		List<StockPriceDTO> stockpriceDtoList = new  ArrayList<StockPriceDTO>() ;
		//采购价格model list		
		List<PBaseStockprice> stockPriceModelList = null;
		
//		//供应商list
//		List<PBaseVerifyprojectSupplier> supplierList = new ArrayList<PBaseVerifyprojectSupplier>();
//		
//		//物料list
//		List<PBaseVerifyprojectMaterial> materialList = new ArrayList<PBaseVerifyprojectMaterial>();
		
		//stockPriceDto为null时查询所有的采购价格资料
		if(pbaseStockPriceModel==null){
			stockPriceModelList = stockPriceDAOProxy.getAllStockPrices();
		}	
		else{//采购价格资料model不为null
			if(pbaseStockPriceModel.getPBaseVerifyprojectMaterialId()!=0&&pbaseStockPriceModel.getPBaseVerifyprojectSupplierId()!=0){
				stockPriceModelList = this.stockPriceDAOProxy.getStockPriceBySupplierIdAndMaterialId(pbaseStockPriceModel.getPBaseVerifyprojectMaterialId(), pbaseStockPriceModel.getPBaseVerifyprojectSupplierId());
			}
			else if(pbaseStockPriceModel.getPBaseVerifyprojectMaterialId()!=0){
				stockPriceModelList = this.stockPriceDAOProxy.getStockPriceByMaterialId(pbaseStockPriceModel.getPBaseVerifyprojectMaterialId()) ;
			}
			else if(pbaseStockPriceModel.getPBaseVerifyprojectSupplierId()!=0){
				stockPriceModelList = this.stockPriceDAOProxy.getStockPriceBySupplierId(pbaseStockPriceModel.getPBaseVerifyprojectSupplierId()) ;
			}
			else if(pbaseStockPriceModel.getAuditState()==0||pbaseStockPriceModel.getAuditState()==1){
				int state = pbaseStockPriceModel.getAuditState() ;
				System.out.println(state);
				stockPriceModelList = stockPriceDAOProxy.getStockPricesByState(state) ;
			}
			else{
				stockPriceModelList = stockPriceDAOProxy.getAllStockPrices();
			}
		}
		
		
//		封装采购价格dto
		if(stockPriceModelList==null)
			return null ;
		for(PBaseStockprice stockpriceModel :stockPriceModelList){
			StockPriceDTO stockPriceDTO = new StockPriceDTO();
			
			//核算项目物料model
			int materialId = stockpriceModel.getPBaseVerifyprojectMaterialId() ;
			PBaseVerifyprojectMaterial materialModel = materialDaoProxy.getMaterialById(materialId) ;
				
			PBaseVerifyprojectSupplier supplierModel = supplierDaoProxy.getSupplierById(stockpriceModel.getPBaseVerifyprojectSupplierId()) ;
			//将采购价格model、供应商model、物料model 封装dto
			stockPriceDTO.setStockpriceModel(stockpriceModel);
			stockPriceDTO.setMaterialModel(materialModel) ;
			stockPriceDTO.setSupplierModel(supplierModel) ;
			//把采购价格dto添加到采购价格dto list中
			stockpriceDtoList.add(stockPriceDTO) ;
		}
		return stockpriceDtoList;
	}
	/**
	 * 修改价格资料
	 */
	@Override
	public void updateStockPrice(PBaseStockprice pbaseStockPriceModel) throws Throwable{
		// TODO Auto-generated method stub
		System.out.println("service++++++++++++++");
		this.stockPriceDAOProxy.updateStockPrice(pbaseStockPriceModel) ;
		System.out.println("service++++++++++++++");
		
	}
	/**
	 * 审核采购价格资料
	 */
	@Override
	public void checkStockPrice(Integer stockPriceId) throws Throwable{
		// TODO Auto-generated method stub
		
		this.stockPriceDAOProxy.checkStockPrice(stockPriceId) ;
	}
	/**
	 * 反审核采购价格资料
	 */
	@Override
	public void unCheckStockPrice(Integer stockPriceId) throws Throwable{
		// TODO Auto-generated method stub
		
		this.stockPriceDAOProxy.uncheckStockPrice(stockPriceId) ;
	}
	/**
	 * 限价--可以针对某一供应商或者所有供应商
	 */
	@Override
	public void controlPrice(PBaseStockprice pbaseStockPriceModel,int flag) throws Throwable{
		// TODO Auto-generated method stub
		//要限价的物料id
		int materialId = pbaseStockPriceModel.getPBaseVerifyprojectMaterialId() ;
		//要限价的供应商id
		int supplierId = pbaseStockPriceModel.getPBaseVerifyprojectSupplierId();
		//最高限价
		float topTip = pbaseStockPriceModel.getTopBid() ;
		if(flag==1){
			System.out.println("flag"+flag);
			this.stockPriceDAOProxy.conTrolPrice(materialId, topTip, supplierId);
		}
		else if(flag==0){
			System.out.println("flag"+flag);
			System.out.println("materialId"+materialId);
			System.out.println("topTip"+topTip);
			this.stockPriceDAOProxy.conTrolPrice(materialId,topTip);
		}
	}
	
	/**
	 * setters and getters for instance variables 
	 * @return
	 */
	
	
	public StockPriceDAO getStockPriceDAOProxy() {
		return stockPriceDAOProxy;
	}
	public void setStockPriceDAOProxy(StockPriceDAO stockPriceDAOProxy) {
		this.stockPriceDAOProxy = stockPriceDAOProxy;
	}
	/**
	 * @return the supplierDaoProxy
	 */
	public PBaseVerifyprogectSupplierDao getSupplierDaoProxy() {
		return supplierDaoProxy;
	}
	/**
	 * @param supplierDaoProxy the supplierDaoProxy to set
	 */
	public void setSupplierDaoProxy(PBaseVerifyprogectSupplierDao supplierDaoProxy) {
		this.supplierDaoProxy = supplierDaoProxy;
	}
	/**
	 * @return the materialDaoProxy
	 */
	public PBaseVerifyprojectMaterialDao getMaterialDaoProxy() {
		return materialDaoProxy;
	}
	/**
	 * @param materialDaoProxy the materialDaoProxy to set
	 */
	public void setMaterialDaoProxy(PBaseVerifyprojectMaterialDao materialDaoProxy) {
		this.materialDaoProxy = materialDaoProxy;
	}
	
}
